﻿@{
    ViewBag.Title = "附件";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    var keyValue = $.request("keyValue");
    var photoableId = $.request("photoable_id");
    var photoableType = $.request("photoable_type");
    $(function () {
        if (!!photoableId && !!photoableType) {
            $("#photoable_type").val(photoableType);
            $("#photoable_id").val(photoableId);
        }

        if (!!keyValue) {
            $.ajax({
                url: "/SiteManage/Site/GetFormJson",
                data: { keyValue: keyValue },
                dataType: "json",
                async: false,
                success: function (data) {
                    $("#form1").formSerialize(data);
                    console.log(data);
                    if ($.trim(data.F_AttachmentPpt)) {
                        console.log('PPT存在')
                        $("#F_AttachmentPptA").show().attr('href', data.F_AttachmentPpt);
                    }
                    if ($.trim(data.F_Attachment)) {
                        console.log('Pdf存在')

                        $("#F_AttachmentA").show().attr('href', data.F_Attachment);
                    }
                }
            });
        }
    });
    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }
        $.submitForm({
            url: "/SiteManage/Site/SubmitAttachmentForm?keyValue=" + keyValue,
            param: $("#form1").formSerialize(),
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }
</script>

<form id="form1">
    <div style="padding-top: 20px; margin-right: 20px;">
        <table class="form">
          
            <tr>
                <th class="formTitle">附件PDF</th>
                <td class="formValue">
                   
                    <a target="_blank" href="javascript:void(0)" id="F_AttachmentA" style="display:none">点击查看->PDF</a>
                </td>
             
            </tr>


            <tr>
                  <th class="formTitle">附件PPT</th>
                  <td class="formValue">
                    
                      <a target="_blank"  href="javascript:void(0)" id="F_AttachmentPptA" style="display:none">点击查看->PPT</a>
                  </td>
             </tr>

            <input type="hidden" id="F_Attachment" name="F_Attachment" value="" />
            <input type="hidden" id="F_AttachmentPpt" name="F_AttachmentPpt" value="" />
         
        </table>
    </div>
</form>

<form id="uploader" enctype="multipart/form-data">

    <div style="padding-top: 20px; margin-right: 20px;">
        <table class="form">
            <tr>
                <th class="formTitle">选择PDF</th>
                <td class="formValue">
                    <input id="F_AttachmentFile" type="file"  class="form-control" accept="application/pdf">
                </td>
              
                <td class="formValue">
                    <input type="button" value="上传" class=" btn btn-primary" id="F_AttachmentBtn">
                </td>
                <th class="formTitle"></th>
            </tr>

            <tr>
                <th class="formTitle">选择PPT</th>
                <td class="formValue">
                    <input   type="file" id="F_AttachmentPptFile" class="form-control" accept="application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation  ">
                </td>
                <td class="formValue">
                    <input type="button" value="上传" class="btn btn-primary" id="F_AttachmentPptBtn">
                </td>
                <th class="formTitle"></th>
            </tr>
        </table>
    </div>

</form>
<script>
    $("#F_AttachmentBtn").on('click', function () {
        var argsId = "F_AttachmentFile";
        var setValId = "F_Attachment";
        var hrefId ="F_AttachmentA";
        uploadSend(argsId, setValId,hrefId);

    })
    $("#F_AttachmentPptBtn").on('click', function () {
        var argsId = 'F_AttachmentPptFile';
        var setValId = 'F_AttachmentPpt'
        var hrefId = 'F_AttachmentPptA';
        uploadSend(argsId, setValId, hrefId);
    })
    function uploadSend(argsId, setValId, hrefId) {
        var data = new FormData();
        var item = document.getElementById(argsId);


        if (item != null) {
            var file = item.files[0];
            fileSize = file.size;
            fileSize = Math.round(fileSize / 1024 * 100) / 100; //kb
            if (fileSize > 30720) {
                alert('该附件超过30mb，上传失败请保持在30mb以内！');
                return false;
            }
            data.append(file.name, file);
            var xhr = new XMLHttpRequest();
            xhr.onload = function () {
                data = JSON.parse(xhr.responseText);
                if (data.state == 1) {
                    console.log(data);
                    document.getElementById(setValId).value = data.data;
                    document.getElementById(hrefId).href = data.data;
                    document.getElementById(hrefId).style.display = 'block'
                }
                alert(data.msg);
            };
            xhr.onerror = function (err) {
                console.error(err);
                document.getElementById(setValId).value = '';
            };
            xhr.open('post', '/SiteManage/Site/UploadAttachment', true);
            xhr.send(data);
        }
    }
</script>

